package map;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;

//高德地图
@Slf4j
public class AMapUtils {
//    c018debba4451fe02ebc82827a15e674


    //获取两个坐标的骑行距离
    //http://restapi.amap.com/v4/direction/bicycling?origin=117.108798,36.693084&destination=117.080734,36.652733&key=c018debba4451fe02ebc82827a15e674
    public static String getRideDistinct(String key, Double sLat, Double sLng, Double eLat, Double eLng) {
        if (null == sLat || null == sLng || null == eLat || null == eLng) {
            return "";
        }
        try {
            String s = HttpRequest.sendGet("http://restapi.amap.com/v4/direction/bicycling", "origin=" + sLng + "," + sLat + "&destination=" + eLng + "," + eLat + "&key=" + key);

            if (StringUtils.isNotEmpty(s)) {
                Map map = JSON.parseObject(s, Map.class);
                Map data = (Map)map.get("data");
                if (data!=null){
                    List path = (List) data.get("paths");
                    if (path!=null&&path.size()>0){
                        Map path1 = (Map) path.get(0);
                        String distance = String.valueOf(path1.get("distance"));
                        if (distance!=null) {
                            return distance;
                        }
                    }
                }
                String errdetail = (String) map.get("errdetail");
                log.info(errdetail);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getRideDistinct(Double sLat, Double sLng, Double eLat, Double eLng) {
        if (null == sLat || null == sLng || null == eLat || null == eLng) {
            return "";
        }
        try {
            String s = HttpRequest.sendGet("http://restapi.amap.com/v4/direction/bicycling", "origin=" + sLng + "," + sLat + "&destination=" + eLng + "," + eLat + "&key=" + "5b9e41e21963210d9b2216d56a45d93a");

            if (StringUtils.isNotEmpty(s)) {
                int start = s.indexOf("paths\":[{\"distance") + 20;
                int end = s.indexOf(",\"duration\":");
                return s.substring(start, end);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }




    public static void main(String[] args) {
        //发送 GET 请求
        System.out.println(getRideDistinct("092de382b6bb2c6c714e58579cfd8086", 23.0333350, 113.7678430, 22.5767590, 113.9519150));
//
//        //发送 POST 请求
//        String sr=HttpRequest.sendPost("http://localhost:6144/Home/RequestPostString", "key=123&v=456");
//        System.out.println(sr);
    }
}
